import TIM from 'tim-wx-sdk';
import TIMUploadPlugin from 'tim-upload-plugin';
import TIMProfanityFilterPlugin from 'tim-profanity-filter-plugin';

const api = require('../config/api.js');
var app = getApp();

function imInit() {
  try {
    let config = {
      userID: '', //User ID
      SDKAPPID: api.tmAppId, // Your SDKAppID
      SECRETKEY: api.tmSecretKey, // Your secretKey
      EXPIRETIME: 604800,
    }
    if (app.globalData.hasLogin) {

      let userInfo = wx.getStorageSync('userInfo');

      console.log(userInfo)

      config.userID = userInfo.tmUserId;

      let userSig = userInfo.tmUserSign;

      wx.$TUIKit = TIM.create({
        SDKAppID: config.SDKAPPID
      })
      wx.$chat_SDKAppID = config.SDKAPPID;
      wx.$chat_userID = config.userID;
      wx.$chat_userSig = userSig;
      wx.$TUIKitTIM = TIM;
      wx.$TUIKit.registerPlugin({
        'tim-upload-plugin': TIMUploadPlugin
      });
      wx.$TUIKit.registerPlugin({
        'tim-profanity-filter-plugin': TIMProfanityFilterPlugin
      });
      wx.$TUIKit.login({
        userID: config.userID,
        userSig
      });
      wx.setStorage({
        key: 'currentUserID',
        data: [],
      });

    }
  } catch (e) {
    console.error(e)
  }

}



module.exports = {
  imInit: imInit,
}